package film

import (
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	FilmModel "jy_go/app/admin/model/film"
	"jy_go/common"
	"jy_go/db"
	"jy_go/jy_log"
	"math"
	"net/http"
	"strconv"
)


var (
	Db = db.GetDb()
)

// @Summary 电影网址列表
// @Description 电影网址列表
// @Tags 电影网址列表
// @Accept mpfd
// @Param limit query int false "当前显示的条数，默认10"
// @Param page query int false "当前页，默认1"
// @Success 200 {string} string "{"message": "查询成功","code":"success","data":"数据","limit":10,"page":1,"count":10}"
// @Failure 400 {string} string "{"message": "错误传参","code":"error"}"
// @Router /admin/film_lst [post]
func Lst(c *gin.Context){
	Limit, err := strconv.Atoi(c.DefaultPostForm("limit", "10"))

	if err != nil{
		c.JSON(400,gin.H{"code":"error","message":"错误传参"})
		return
	}

	Page,err := strconv.Atoi(c.DefaultPostForm("page", "1"))
	if err != nil{
		c.JSON(400,gin.H{"code":"error","message":"错误传参"})
		return
	}

	//初始化结构体
	var data = make([]FilmModel.Film, 0)
	var model FilmModel.Film
	var count int64
	dbtitle := Db.Model(&model)
	if err := dbtitle.Count(&count).Error;err != nil{
		if errors.Is(err,gorm.ErrRecordNotFound){
			c.JSON(200,gin.H{"code":"success","message":"暂无数据","data":data,"limit":Limit,"page":Page,"count":0})
			return
		}
		go jy_log.JyLog("查询失败，err：" + err.Error())
		c.JSON(400,gin.H{"code":"error","message":"查询失败"})
		return
	}
	dbtitle.Limit(Limit).Offset((Page - 1) * Limit).Find(&data)

	c.JSON(200,gin.H{"code":"success","message":"查询成功","data":data,"limit":Limit,"page":Page,"count":math.Ceil(float64(count) / float64(Limit))})
	return
}

// @Summary 电影网址添加
// @Description 电影网址添加
// @Tags 电影网址添加
// @Accept mpfd
// @Param name query string true "网站名称"
// @Param url query string true "网址"
// @Success 200 {string} string "{"message": "添加成功","code":"success"}"
// @Failure 400 {string} string "{"message": "添加失败","code":"error"}"
// @Router /admin/film_add [post]
func Add(c *gin.Context)  {
	var form FilmModel.Film

	if err := c.Bind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err})
		return
	}

	//验证数据
	if err := common.Validate(&form); err != ""{
		c.JSON(400,gin.H{"code":"error","message":err})
		return
	}

	if err := Db.Create(&form).Error;err != nil {
		c.JSON(400,gin.H{"code":"error","message":"添加失败"})
		go jy_log.JyLog("添加失败，err：" + err.Error())
		return
	}

	c.JSON(200,gin.H{"code":"error","message":"添加成功"})
	return
}

// @Summary 电影网址修改
// @Description 电影网址修改
// @Tags 电影网址修改
// @Accept mpfd
// @Param id query string true "id"
// @Param name query string true "网站名称"
// @Param url query string true "网址"
// @Success 200 {string} string "{"message": "修改成功","code":"success"}"
// @Failure 400 {string} string "{"message": "修改失败","code":"error"}"
// @Router /admin/film_upd [post]
func Upd(c *gin.Context)  {
	//接收数据
	var form FilmModel.Film

	if err := c.Bind(&form); err != nil {
		c.JSON(400,gin.H{"code":"success","message":"请填写要修改的内容"})
		return
	}

	//验证数据
	if err := common.Validate(&form); err != ""{
		c.JSON(400,gin.H{"code":"error","message":err})
		return
	}

	if err := Db.Where("id = ?",form.ID).Updates(&form).Error;err != nil {
		c.JSON(400,gin.H{"code":"error","message":"修改失败"})
		go jy_log.JyLog("修改失败，err：" + err.Error())
		return
	}
	c.JSON(200,gin.H{"code":"success","message":"修改成功"})
	return
}

// @Summary 电影网址删除
// @Description 电影网址删除
// @Tags 电影网址删除
// @Accept mpfd
// @Param id query string true "id"
// @Success 200 {string} string "{"message": "删除成功","code":"success"}"
// @Failure 400 {string} string "{"message": "删除失败","code":"error"}"
// @Router /admin/film_del [post]
func Del(c *gin.Context)  {
	id := c.DefaultPostForm("id", "")
	if id == "" {
		c.JSON(400, gin.H{"code": "error", "message": "请选择要删除的管理员"})
		return
	}

	if err := Db.Delete(&FilmModel.Film{}, id).Error;err != nil{
		c.JSON(400,gin.H{"code":"error","message":"删除失败"})
		go jy_log.JyLog("删除失败，err：" + err.Error())
		return
	}
	c.JSON(200,gin.H{"code":"success","message":"删除成功"})
	return
}













